package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCouponMapper;
import com.qf.pojo.DtsCoupon;
import com.qf.service.DtsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * projectName:workspace_6
 * author:hyn
 * time:2021/12/1 22:05
 * description:
 */
@Service
public class DtsCouponServiceImpl implements DtsCouponService {

    @Autowired
    private DtsCouponMapper dtsCouponMapper;
    @Override
    public IPage<DtsCoupon> findPage(Integer page, Integer limit, String name, Integer type, String status, String sort, String order) {
        IPage<DtsCoupon> ipage = new Page<>(page, limit);

        QueryWrapper<DtsCoupon> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(name)){
            wrapper.like("name", name);
        }

        if(!StringUtils.isEmpty(type)){
            wrapper.eq("type", type);
        }

        if(!StringUtils.isEmpty(status)){
            wrapper.eq("status", status);
        }

        if(order.equalsIgnoreCase("asc")){
            wrapper.orderByAsc(sort);
        }else{
            wrapper.orderByDesc(sort);
        }

        ipage = dtsCouponMapper.selectPage(ipage,wrapper);

        return ipage;
    }

    @Override
    public DtsCoupon selectById(Integer id) {
        return dtsCouponMapper.selectById(id);
    }

    @Override
    public DtsCoupon insert(DtsCoupon dtsCoupon){
        dtsCoupon.setAddTime(new Date());
        dtsCoupon.setUpdateTime(new Date());
        dtsCouponMapper.insert(dtsCoupon);
        return dtsCoupon;
    }

    @Override
    public DtsCoupon update(DtsCoupon dtsCoupon){
        dtsCouponMapper.deleteById(dtsCoupon.getId());
        dtsCoupon.setAddTime(new Date());
        dtsCoupon.setUpdateTime(new Date());
        dtsCouponMapper.insert(dtsCoupon);
        return dtsCoupon;
    }

    @Override
    public void delete(DtsCoupon dtsCoupon) {
        dtsCouponMapper.deleteById(dtsCoupon.getId());
    }

    @Override
    public DtsCoupon findByName(DtsCoupon dtsCoupon) {
        LambdaQueryWrapper<DtsCoupon> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCoupon::getName,dtsCoupon.getName());
        return dtsCouponMapper.selectOne(wrapper);
    }

    @Override
    public DtsCoupon findById(DtsCoupon dtsCoupon) {
        LambdaQueryWrapper<DtsCoupon> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCoupon::getId,dtsCoupon.getId());
        DtsCoupon dtsCoupon1 = dtsCouponMapper.selectOne(wrapper);
        return dtsCoupon1;
    }

}
